import templateItems from "../commonComponents/templateItems.vue"

export default {
  data() {
    return {
      submitList: [],
    }
  },
  methods: {
    init() {

      this.lastFlag = this.$store.state.headerRightType;

      this.$store.commit("_regionDataChecked", {
        id: [],
        text: "点击选择发布单位"
      });

      _data.planUnitsChecked = [];

      this.$store.commit("_feedBackItemCheck", []);

      _methods.headerRightShow();

      this.getDom();

      this.getOrgList();

      this.title = _methods.getUrlParam("title") - 0;

      _methods.headerTitleChange(this.title);

      this.getConfig();
    },
    getDom() {
      let titleKey = {
        label: "标题",
        id: "title",
        singLine: 1,
        type2: 1,
        must: 1,
        talk: 0,
      };

      let sendDept = {
        label: "报送单位",
        singLine: 1,
        type2: 2,
        must: 0,
        talk: 0,
        value: _data.userInfo.deptName,
        value2: _data.userInfo.deptId,
        id: "sendDeptid",
        dv: 1,
      };

      let sendPeople = {
        label: "填报人",
        singLine: 1,
        type2: 2,
        must: 0,
        talk: 0,
        value: _data.userInfo.name,
        value2: _data.userInfo.userId,
        id: "sendUserid",
        dv: 1,
      };

      this.submitList = [
        {
          title: "基本信息",
          list: {
            title: titleKey,
            sendDeptId:sendDept,
            sendUserId:sendPeople,
            beginDate: {
              label: "报告开始时间",
              singLine: 1,
              type2: 6,
              must: 1,
              talk: 0,
              value: "",
              dateType: "date",
              id: "beginDate",
              dv: 1,
            },
            endDate: {
              label: "报告结束时间",
              singLine: 1,
              type2: 6,
              must: 1,
              talk: 0,
              value: "",
              dateType: "date",
              id: "endDate",
              dv: 1,
            },
            beginTime: {
              label: "每天发布时间",
              singLine: 1,
              type2: 6,
              must: 1,
              talk: 0,
              value: "",
              dateType: "time",
              id: "beginTime",
              dv: 1,
            },
            endTime: {
              label: "每天反馈截止时间",
              singLine: 1,
              type2: 6,
              must: 1,
              talk: 0,
              value: "",
              dateType: "time",
              id: "endTime",
              dv: 1,
            },
          },
        },
      ];
    },
    getDetail() {
      if (this.title == 122) {

        let id = _methods.getUrlParam("id");

        _ajax.withLoading(_ajaxUrls.planDetail.url + id, {
            id: id
          },
          (ajData) => {

            let data = ajData.data;

            let obj = {};
            data.configIds.split(",").forEach(i => {
              obj[i] = true;
            });

            this.getConfig(obj);

            for (let key in data) {
              _methods.sVal(key, data[key]);
            }

            _data.planUnitsChecked = data.deptIdsSpecial.split(",");

          }, _ajaxUrls.planDetail.type);
      }
    },
    getConfig(configIdObj = {}) {
      _ajax.noLoading(_ajaxUrls.planConfigGet.url, {
          pageSize: 1000,
        },
        (ajData) => {

          let list = [];

          if (this.title === 122) {
            let arr = [];

            list = ajData.data.list.map(i => {
              if (configIdObj[i.configId]) {
                arr.push(i.configId);
              }
              i.title = i.configName;
              return i;
            });
            let ids = arr.join(",");

            this.configDetail(ids);

          } else {
            list = ajData.data.list.map(i => {
              i.title = i.configName;
              return i;
            });
          }

          _data.planFeedBack = list;

        }, _ajaxUrls.planConfigGet.type);

    },
    configDetail(id) {
      _ajax.noLoading(_ajaxUrls.planConfigSearch.url, {
          configIds: id,
        },
        (ajData) => {
          let list = ajData.data.map(i => {
            i.title = i.configName;
            return i;
          });

          this.$store.commit("_feedBackItemCheck", list);

        }, _ajaxUrls.planConfigSearch.type);
    },
    goConfig() {
      _methods.goPath(`/planList/feedBackItem?type=120&isCheckId=1`);
    },
    _deleteConfig(index) {
      this.$store.state.feedBackItemCheck.splice(index, 1);
    },
    goOrgList(type) {
      _methods.goPath(`/planList/planAdd/orgList?type=${type}`);
    },
    getOrgList() {
      _ajax.noLoading(_ajaxUrls.planUnits.url, {},
        (ajData) => {

          let data = ajData.data.CII;
          let arr = ajData.data.NOTCII;

          data.forEach(i => {
            arr.push(i);
          });

          _data.planUnits = arr;

        }, _ajaxUrls.planUnits.type);
    },
    submitPlan() {

      new Promise((resolve, reject) => {
        //验证基本信息

        let obj = {};

        let base = this.submitList[0].list;

        for (let key in base) {
          let val = base[key];
          let data;
          if (val.dv === 1) {
            data = _methods.getAttribute(val.id);
          } else {
            data = _methods.getValue(val.id);
          }
          if (val.must === 1 && !data) {
            reject(val.label);
          } else {
            obj[key] = data;
          }
        }

        let configids = this.$store.state.feedBackItemCheck.map(i => {
          return i.configId;
        }).join(",");

        let planUnitsChecked = _data.planUnitsChecked.join(",");

        let base2 = {
          configIds: {
            label: "反馈项",
            value: configids,
          },
          require: {
            label: "报告要求",
            value: _methods.getValue("require"),
          },
          deptIdsSpecial: {
            label: "发布单位",
            value: planUnitsChecked,
          },
        };

        for (let key in base2) {
          let val = base2[key];
          let data = val.value;
          if (!data) {
            reject(val.label);
          } else {
            obj[key] = data;
          }
        }

        resolve(obj);
      })
        .then((obj) => {
            let data = obj;

            let type = {
              121: "post",
              122: "put",
            }[this.title];

            data.state = 1;
            data.userId = _data.userInfo.userId;

            _ajax.confirmWithLoading(_ajaxUrls.planAdd.url, data,
              () => {

                _methods.meScrollTriggerDownScroll(_data.planList);

                _alert.alert(() => {
                  _methods.goBack();
                }, '发布成功');

              }, type, '是否确认发布？');

          },
          (label) => {
            mui.toast(`请填写${label}`);
          })
    }
  },
  components: {
    templateItems,
  },
  created() {
    this.init();
  },
  mounted() {
    this.getDetail();
  },
  beforeDestroy() {
    _data.planUnitsChecked = [];
    this.$store.commit("_feedBackItemCheck", []);
    _methods.headerTitleChange(120, true);
    _methods.headerRightShow(this.lastFlag);
  }
}
